<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>图书借阅管理 </title>
    <link rel="stylesheet" type="text/css" href="css/main.css" />
    <script type="text/javascript" src="js/jquery-3.2.1.js"></script>
    <script type="text/javascript">
        $(function() {
            var params = new URLSearchParams(window.location.search);
            var pageNo = params.get("pageNo");
            var likeValue = params.get("likeValue");
            
            // 调用分页查询方法
            queryByPage(pageNo || 1, likeValue || "");
            
            // 点击查询按钮
            $("#search-btn").click(function() {
                var searchValue = $("#search-input").val();
                queryByPage(1, searchValue);
            });
            
            // 回车搜索
            $("#search-input").keypress(function(e) {
                if (e.which == 13) {
                    var searchValue = $(this).val();
                    queryByPage(1, searchValue);
                }
            });
        });
        
        // 分页查询
        function queryByPage(pageNo, likeValue) {
            $("#borrow-table-body").empty();
            $("#borrow-table-body").html("<tr><td colspan='7'>加载中...</td></tr>");
            
            var url = "/librarymanagementsystem/list.borrow";
            var params = {
                "pageNo": pageNo,
                "likeValue": likeValue
            };
            
            $.get(url, params, function(data) {
                if (!data) {
                    alert("获取数据失败，请检查服务器状态！");
                    return;
                }
                
                // 解析返回数据
                var recordList = data.recordList;
                if (!recordList) {
                    $("#borrow-table-body").html("<tr><td colspan='7'>暂无借阅记录</td></tr>");
                    return;
                }
                
                // 清空表格内容
                $("#borrow-table-body").empty();
                
                // 遍历记录并添加到表格
                for (var i = 0; i < recordList.length; i++) {
                    var record = recordList[i];
                    var id = record.id;
                    var bookTitle = record.book_title || record.bookTitle || record.title || "未知图书";
                    var readerName = record.reader_name || record.readerName || "";
                    var borrowDate = formatDate(record.borrow_date || record.borrowDate);
                    var returnDate = formatDate(record.return_date || record.returnDate);
                    var actualReturnDate = formatDate(record.actual_return_date || record.actualReturnDate);
                    var status = record.status || "";
                    
                    var statusClass = "";
                    if (status.indexOf("逾期") != -1) {
                        statusClass = "overdue";
                    }
                    
                    var actionBtn = "";
                    if (!actualReturnDate && status !== "已还") {
                        actionBtn = "<button class='btn-small' onclick='returnBook(" + id + ")'>归还</button>";
                    }
                    
                    var row = "<tr>" +
                        "<td>" + id + "</td>" +
                        "<td>" + bookTitle + "</td>" +
                        "<td>" + readerName + "</td>" +
                        "<td>" + borrowDate + "</td>" +
                        "<td>" + returnDate + "</td>" +
                        "<td class='" + statusClass + "'>" + status + "</td>" +
                        "<td>" + actionBtn + "</td>" +
                        "</tr>";
                    
                    $("#borrow-table-body").append(row);
                }
                
                // 更新分页信息
                $("#likeValueStr").val(data.likeValue);
                $("#recordCount").text(data.recordCount);
                $("#totalPages").text(data.totalPages);
                $("#pageNo").text(data.pageNo);
                
                // 更新分页链接
                $("#homePage").attr("href", "borrow_page.html?pageNo=1&likeValue=" + data.likeValue);
                
                if (parseInt(data.pageNo) > 1) {
                    $("#prevPage").attr("href", "borrow_page.html?pageNo=" + (parseInt(data.pageNo) - 1) + "&likeValue=" + data.likeValue);
                    $("#prevPage").removeClass("disabled");
                } else {
                    $("#prevPage").attr("href", "javascript:void(0)");
                    $("#prevPage").addClass("disabled");
                }
                
                if (parseInt(data.pageNo) < parseInt(data.totalPages)) {
                    $("#nextPage").attr("href", "borrow_page.html?pageNo=" + (parseInt(data.pageNo) + 1) + "&likeValue=" + data.likeValue);
                    $("#nextPage").removeClass("disabled");
                } else {
                    $("#nextPage").attr("href", "javascript:void(0)");
                    $("#nextPage").addClass("disabled");
                }
                
                $("#lastPage").attr("href", "borrow_page.html?pageNo=" + data.totalPages + "&likeValue=" + data.likeValue);
            });
        }
        
        // 格式化日期
        function formatDate(dateStr) {
            if (!dateStr) return "";
            
            var date = new Date(dateStr);
            if (isNaN(date.getTime())) {
                return dateStr;
            }
            
            var year = date.getFullYear();
            var month = (date.getMonth() + 1).toString().padStart(2, '0');
            var day = date.getDate().toString().padStart(2, '0');
            
            return year + "-" + month + "-" + day;
        }
        
        // 归还图书
        function returnBook(borrowId) {
            if (confirm("确认归还此图书？")) {
                // 使用表单提交
                var form = $('<form></form>')
                    .attr('method', 'post')
                    .attr('action', 'return.borrow');
                
                // 添加借阅ID
                $('<input />')
                    .attr('type', 'hidden')
                    .attr('name', 'id')
                    .attr('value', borrowId)
                    .appendTo(form);
                
                // 添加返回页面
                $('<input />')
                    .attr('type', 'hidden')
                    .attr('name', 'returnPage')
                    .attr('value', 'borrow_page.html')
                    .appendTo(form);
                
                // 提交表单
                form.appendTo('body').submit();
            }
        }
    </script>
</head>
<body>
    <div class="container">
        <header>
            <h1>图书管理系统</h1>
            <nav>
                <ul>
                    <li><a href="/librarymanagementsystem/index.html">首页</a></li>
                    <li><a href="/librarymanagementsystem/book_page.html">图书管理</a></li>
                    <li><a href="/librarymanagementsystem/borrow_page.html" class="active">借阅管理</a></li>
                </ul>
            </nav>
        </header>
        
        <main>
            <h2>借阅记录查询</h2>
            
            <div class="action-bar">
                <a href="/librarymanagementsystem/borrow_add.html" class="btn">添加借阅</a>
                
                <div class="search-bar">
                    <input type="text" id="search-input" placeholder="输入ID、读者姓名、图书标题或状态...">
                    <button id="search-btn">查询</button>
                </div>
            </div>
            
            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>图书</th>
                        <th>借阅人</th>
                        <th>借阅日期</th>
                        <th>应还日期</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="borrow-table-body">
                    <tr>
                        <td colspan="7">加载中...</td>
                    </tr>
                </tbody>
            </table>
            
            <div class="pagination">
                <span>总记录数: <span id="recordCount">0</span></span>
                <span>总页数: <span id="totalPages">0</span></span>
                <a href="#" id="homePage">首页</a>
                <a href="#" id="prevPage">上一页</a>
                <a href="#" id="nextPage">下一页</a>
                <a href="#" id="lastPage">末页</a>
                <span>当前第 <span id="pageNo">1</span> 页</span>
            </div>
        </main>
        
        <footer>
            <p>&copy; 2025 图书管理系统</p>
        </footer>
    </div>
</body>
</html> 